#!/bin/bash

result=`/usr/bin/mysqladmin ping`
expected='mysqld is alive'

if [[ "$result" != "$expected" ]]
then
echo "It's dead - restart mysql"

# email subject
SUBJECT="[MYSQL ERROR] - Attempting to restart service"

# Email To ?
EMAIL="info@endyourif.com"

# Email text/message
EMAILMESSAGE="/tmp/emailmessage.txt"
echo "$result was received"&gt; $EMAILMESSAGE
echo "when we were expected $expected" &gt;&gt;$EMAILMESSAGE
# send an email using /bin/mail
mail -s "$SUBJECT" "$EMAIL" &lt; $EMAILMESSAGE

sudo /etc/init.d/mysql restart
fi